﻿
namespace TimetableCSP.Scheduler.Entities
{
	/// <summary>
	/// This constraint guarantees that the classroom can accommodate the number of students in the class.
	/// </summary>
	public class ClassroomSizeConstraint : UnaryConstraint
	{
		private Classroom[] classrooms;

		/// <summary>
		/// Initializes a new instance of the <see cref="ClassroomSizeConstraint"/> class.
		/// </summary>
		/// <param name="classrooms">The array of all classrooms.</param>
		public ClassroomSizeConstraint(Classroom[] classrooms)
		{
			this.classrooms = classrooms;
		}

		/// <summary>
		/// Determines whether the supplied variable violates the constraint.
		/// </summary>
		/// <param name="a">The variable.</param>
		/// <param name="conflictingAttributes">If there's a conflict, then this array holds the names of those attributes that need to be modified in order to solve the conflict.</param>
		/// <returns>
		///   <c>true</c> if there's a conflict; otherwise, <c>false</c>.
		/// </returns>
		public override bool IsConflict(AbstractVariable a, out string[] conflictingAttributes)
		{
			conflictingAttributes = null;
			ClassVariable classA = (ClassVariable)a;

			if (classA.NumberOfStudents > classrooms[classA.Classroom].Capacity)
			{
#if DEBUG_EXTRA
				System.Diagnostics.Debug.WriteLine("Classroom size conflict");
				System.Diagnostics.Debug.WriteLine("\tA: " + classA);
				System.Diagnostics.Debug.WriteLine("\tClassroom size: " + classrooms[classA.Classroom].Capacity );
#endif
				conflictingAttributes = new string[] { ClassVariable.ClassroomAttributeName };
				ConflictCount++;
				return true;
			}
			return false;
		}
	}
}
